// 为元素添加class
export function addClass(el, className) {
  if (hasClass(el, className)) {
    return
  }
  let newClass = el.className.split(' ')
  newClass.push(className)
  el.className = newClass.join(' ')
}

// 判断元素是否有该class
export function hasClass(el, className) {
  let reg = new RegExp('(^|\\s)' + className + '(\\s|$)')
  return reg.test(el.className)
}

// 获取元素的data-index属性值，listview组件有用到
export function getData(el, name, val) {
  const prefix = 'data-'
  const _name = prefix + name
  if (val) {
    return el.setAttribute(_name, val)
  } else {
    return el.getAttribute(_name)
  }
}


// 原生浏览器的样式api
let elementstyle = document.createElement('div').style

// 自动检测是哪个厂商
let vendor = (() => {
  let transformNames = {
    webkit: 'webkitTransfrom',
    Moz: 'MozTransfrom',
    O: 'OTransfrom',
    ms: 'msTransfrom',
    standard: 'transfrom',
  }

  for (let key in transformNames) {
    if (elementstyle[transformNames[key]] !== undefined) {
      return key
    }
  }

  return false
})()

// 自动返回样式并带上厂商前缀
export function prefixStyle(style) {
  if (vendor === false) {
    return false
  }

  if (vendor === 'standard') {
    return style
  }

  // 返回厂商前缀加样式首字母开头，如：webkitTransfrom
  return vendor + style.charAt(0).toUpperCase() + style.substr(1)
}